package com.spring.dynamicdatasource.utils;

import org.apache.catalina.util.ParameterMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

/**
 * Request请求参数重置值
 *
 * @author HouKunLin
 * @date 2019/9/21 0021 0:18
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
    private ParameterMap<String, String[]> parameters = new ParameterMap<>();

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request The request to wrap
     * @throws IllegalArgumentException if the request is null
     */
    public ParameterRequestWrapper(HttpServletRequest request) {
        super(request);
        parameters.putAll(request.getParameterMap());
    }

    @Override
    public String getParameter(String name) {
        String[] value = parameters.get(name);
        if (value == null) {
            return null;
        }
        return value[0];
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return parameters;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(parameters.keySet());
    }

    @Override
    public String[] getParameterValues(String name) {
        return parameters.get(name);
    }

    public void putParameter(String key, String... values) {
        parameters.put(key, values);
    }
}
